<?php
namespace app\common\service;

use think\Db;

class Map extends Base {

    public function getset(){
        $key = tpCache('basic.qq_map_key');
        $result = [
            'status' => 1,
            'msg' => '获取成功',
            'data' => $key,
        ];
        return $result;
    }

    //商铺搜索
    public function query_adr(){
        $keyword = input('keyword');
        $lng = input('longitude');
        $lat = input('latitude');
        $radius = input('radius')?:'1000';
        $address = input('address');
        $res = logic('MapQQ')->searchNearbyPlace($keyword,['lat'=>$lat,'lng'=>$lng],$radius,1);
        if($res['status']==0){
            return ['status'=>0,'msg'=>$res['message']];
        }
        $lists = $res['data'];
        $title = ['商户名称','商户类型','地址','电话','距离'];
        $data = [];
        foreach($lists as $v){
            $arr = [
                $v['title'],$v['category'],$v['address'],$v['tel'],$v['_distance']
            ];
            $data[] = $arr;
        }
        $excel_file = export_excel($title,$data);
        //插入订单记录
        $map_set = Db::name('map_set')->where('1=1')->find();
        $pay_money = $map_set['pay_money'];
        $order_data = [
            'user_id' => $this->user_id,
            'keyword' => $keyword,
            'address' => $address,
            'lng' => $lng,
            'lat' => $lat,
            'radius' => $radius,
            'data_count' => count($lists),
            'file_path' => $excel_file,
            'w_time' => time(),
            'pay_money' => $pay_money,
        ];
        Db::name('map_order')->insert($order_data);
        $result = [
            'status' => 1,
            'msg' => $res['message'],
            'datalist' => $res['data'],
            'file_paht' => $excel_file,
        ];
        return $result;
    }
    //地址搜素
    public function query_adr_by_keyword(){
        $keyword = input('keyword');
        $res = logic('MapQQ')->addressToLocation($keyword);
        return $res;
    }

    public function getLists(){
        //分页数据
        $pagenum = input('post.pagenum');
        if(!$pagenum) $pagenum = 1;
        $pernum = 20;
        $st = input('st');
        //查询条件
        $where = [];
        if($st!='all'){
            $where['order_status'] = $st;
        }
        $datalist = Db::name('map_order')
            ->where($where)
            ->page($pagenum,$pernum)
            ->order('id desc')
            ->field('*')
            ->select();

        return ['status'=>1,'data'=>$datalist];
    }
    //获取订单详情
    public function getDetail(){
        $oid = input('id');
        $order = Db::name('map_order')->where('id',$oid)->find();
        if($order['order_status']==1){
            //已解析
            $res = [
                'status' => 1,
                'file_path' => $order['file_path']
            ];
        }else{
            if($order['pay_money']>0){
                $res = qianbao($this->user_id,-$order['pay_money'],$this->user_id,1,'视频解析订单退款,订单号: '.$order['ordernum']);
                if(!$res['status']){
                    return ['status'=>0,'msg'=>'支付失败，'.$res['msg']];
                }
            }
            Db::name('map_order')->where('id',$oid)->update(['order_status'=>1]);
            //已解析
            $res = [
                'status' => 1,
                'file_path' => $order['file_path']
            ];
        }
        return $res;
    }
    public function delOrder(){
        $oid = input('id');
        $order = Db::name('map_order')->where('id',$oid)->find();
        if(!$order){
            return ['status'=>0,'msg'=>'数据不存在'];
        }
        Db::name('map_order')->where('id',$oid)->delete();
        return ['status'=>1,'msg'=>'删除成功'];
    }
}
